﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using DTC.Database;

namespace DTC.Converters
{
    [ValueConversion(typeof(string), typeof(DTCDisease))]
    public class DTCDiseaseToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture) { return null; }
        public object Convert(object value)
        {
            if (value == null)
                return null;
            if(!(value is string))
                return null;
            string CodeStr= "";
            string NameStr = "";
            string[]  SubStrings=(value as string).Split(new char[] { '-' });
            if (SubStrings.Count() > 0)
            {
                CodeStr = SubStrings[0];
                CodeStr = CodeStr.Trim();
            }
            if (SubStrings.Count() > 1)
            {
                NameStr = SubStrings[1];
                NameStr = NameStr.Trim();
            }
            if (CodeStr.Length == 0 && NameStr.Length == 0)
                return null;
            return new DTCDisease() { Code = CodeStr, Name = NameStr };

        }
        public object ConvertBack(object value, Type targetType, object parameter,
            CultureInfo culture) { return null; }
        public object ConvertBack(object value)
        {
            if (value == null)
                return null;
            if (!(value is DTCDisease))
                return null;
            string result = (value as DTCDisease).Code + " - " + (value as DTCDisease).Name;
            return result;
        }
    }
}